package com.example.materialtest;

import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    //使用Toolbar首先先把AndroidManifest.xml中的android:theme="@style/AppTheme主题的
    // parent设置为="Theme.AppCompat.Light.NoActionBar"或者Theme.AppCompat.NoActionBar只不过后者是深色主题一般建议使用前者
    private DrawerLayout drawerLayout;
    private Furit[] furits={new Furit("苹果",R.drawable.ic_pingguo)
            ,new Furit("菠萝",R.drawable.ic_boluo)
            ,new Furit("草莓",R.drawable.ic_caomei)
            ,new Furit("橘子",R.drawable.ic_juzi)
            ,new Furit("芒果",R.drawable.ic_mangguo)
    };
    private List<Furit> furitslist=new ArrayList<>();
    private FuritAdapter adapter;
    private SwipeRefreshLayout swi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar=(Toolbar)findViewById(R.id.toobar);
        setSupportActionBar(toolbar);
        drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
        NavigationView navigationView=(NavigationView)findViewById(R.id.nav_view);
        ActionBar actionBar=getSupportActionBar();
        //分别得到DrawerLayout，ActionBar的实例对象
        if (actionBar!=null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            //setDisplayHomeAsUpEnabled让导航按钮显示出来
            actionBar.setHomeAsUpIndicator(R.mipmap.ic_launcher_round);
            //为导航按钮设置一个图标
            //左边导航按钮名字是HomeAsUp
        }
        navigationView.setCheckedItem(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.nav_1:
                        Toast.makeText(MainActivity.this,"11111",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.nav_2:
                        Toast.makeText(MainActivity.this,"22222",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.nav_3:
                        Toast.makeText(MainActivity.this,"33333",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.nav_4:
                        Toast.makeText(MainActivity.this,"44444",Toast.LENGTH_LONG).show();
                        break;
                }
                return true;
            }
        });
        FloatingActionButton floatingActionButton=(FloatingActionButton)findViewById(R.id.fab);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view,"你确定要删除数据",Snackbar.LENGTH_LONG).setAction("确认", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this,"文件已经删除",Toast.LENGTH_LONG).show();
                    }
                }).show();

            }
        });
        initFurits();
        RecyclerView re=(RecyclerView)findViewById(R.id.recycler_view);
        GridLayoutManager lamanger=new GridLayoutManager(this,2);
        //GridLayoutManager这中布局接收两个参数 context 第二个是要显示的列数
        re.setLayoutManager(lamanger);
        adapter=new FuritAdapter(furitslist);
        re.setAdapter(adapter);
        swi=(SwipeRefreshLayout)findViewById(R.id.swipe_refresh);
        //得到SwipeRefreshLayout实例
        swi.setColorSchemeResources(R.color.colorPrimary);
        //调用setColorSchemeResources设置刷新进度条的颜色
        swi.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            //刷新事件的监听
            //然后会回调onRefresh方法在这个方法中可以处理具体的刷新逻辑
            @Override
            public void onRefresh() {
                refreFurit();
            }
        });
    }

    private void refreFurit() {
        new Thread(new Runnable() {
            //开启一个线程
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                }catch (Exception e){
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    //回到主线程
                    @Override
                    public void run() {
                        initFurits();
                        //重新生成数据
                        adapter.notifyDataSetChanged();
                        //调用adapter的notifyDataSetChanged方法通知数据发生了变换
                        swi.setRefreshing(false);
                        //用于表示刷新事件结束 并隐藏刷新进度条
                    }
                });
            }
        }).start();
    }

    private void initFurits() {
        furitslist.clear();
        for (int i=0;i<50;i++){
            Random ra=new Random();
            int in=ra.nextInt(furits.length);
            furitslist.add(furits[in]);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar,menu);
        //获取一个getMenuInflater加载Menu布局
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //设置Menu菜单中的每一个菜单项的点击事件
        switch (item.getItemId()){
            case android.R.id.home:
                //这里需要注意setHomeAsUpIndicator的id永远都是android.R.id.home
                drawerLayout.openDrawer(GravityCompat.START);
                break;
            case R.id.backup:
                Toast.makeText(MainActivity.this,"Backup",Toast.LENGTH_LONG).show();
                break;
            case R.id.delete:
                Toast.makeText(MainActivity.this,"Delete",Toast.LENGTH_LONG).show();
                break;
            case R.id.settings:
                Toast.makeText(MainActivity.this,"Setting1",Toast.LENGTH_LONG).show();
                break;
            case R.id.ss:
                Toast.makeText(MainActivity.this,"Setting2",Toast.LENGTH_LONG).show();
                break;

        }
        return true;
    }
}
